<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- /课程列表 开始 -->
    <section class="container">
      <header class="comm-title">
        <h2 class="fl tac">
          <span class="c-333">全部课程</span>
        </h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">课程类别</span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li>
                  <a title="全部" href="/course">全部</a>
                </li>

                <li v-for="(onesubject,index) in subjectList" :key="index" :class="{active:oneIndex==index}">
                  <a :title="onesubject.label" href="#" @click="selectOneSubject(onesubject.id,index)" >{{onesubject.label}}</a>
                </li>
                
              </ul>
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14"></span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">

                <li v-for="(twosu,index) in twoSubject" :key="index" :class="{active:twoIndex==index}">
                  <a :title="twosu.label" href="#" @click="selectTwoSubject(twosu.id,index)">{{twosu.label}}</a>
                </li>
                
              </ul>
            </dd>
          </dl>
          <div class="clear"></div>
        </section>
        <div class="js-wrap">
          <section class="fr">
            <span class="c-ccc">
              <i class="c-master f-fM">1</i>
              /
              <i class="c-666 f-fM">1</i>
            </span>
          </section>
          <section class="fl">
            <ol class="js-tap clearfix">
             <li :class="{'current bg-orange':this.courseQuery.viewCount!=''}">
                <a title="销量" href="javascript:void(0);" @click="searchBuyCount()">销量
                <span :class="{hide:this.courseQuery.viewCount==''}">↓</span>
                </a>
              </li>
              <li :class="{'current bg-orange':this.courseQuery.gmtCreate!=''}">
                <a title="最新" href="javascript:void(0);" @click="searchGmtCreate()">最新
                <span :class="{hide:this.courseQuery.gmtCreate==''}">↓</span>
                </a>
              </li>
              <li :class="{'current bg-orange':this.courseQuery.price!=''}">
                <a title="价格" href="javascript:void(0);" @click="searchPrice()">价格&nbsp;
                  <span :class="{hide:this.courseQuery.price==''}">↓</span>
                </a>
              </li>
            </ol>
          </section>
        </div>
        <div class="mt40">
          <!-- /无数据提示 开始-->
          <section class="no-data-wrap"  v-if="this.courseMap.total === 0">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <span class="c-666 fsize14 ml10 vam"
              >没有相关数据，小编正在努力整理 中...</span
            >
          </section>
          <!-- /无数据提示 结束-->
          <article class="comm-course-list">
            <ul class="of" id="bna">
              <li v-for="course in courseMap.records" :key="course.id">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img
                      :src="course.cover"
                      class="img-responsive"
                      :alt="course.title"
                    />
                    <div class="cc-mask">
                      <a
                        :href="'/course/'+course.id"
                        :title="course.title"
                        class="comm-btn c- btn-1"
                        >开始学习</a
                      >
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a
                      :href="'/course/'+course.id"
                      :title="course.title"
                      class="course-title fsize18 c-333"
                      >{{course.title}}</a
                    >
                  </h3>
                  <section class="mt10 hLh20 of">
                    
                    <span class="fr jgTag bg-green" v-if="Number(course.price) ===0">
                        <i class="c-fff fsize12 f-fA">免费</i>
                      </span>
                      <span class="fr jgTag bg-green" v-else> 
                        <i class="c-fff fsize12 f-fA"> ￥{{course.price}}</i>
                      </span> 

                    <span class="fl jgAttr c-ccc f-fA">
                      <i class="c-999 f-fA">{{course.buyCount}}人学习</i>
                      |
                      <i class="c-999 f-fA">{{course.viewCount}}评论</i>
                    </span>
                  </section>
                </div>
              </li>
             
            </ul>
            <div class="clear"></div>
          </article>
        </div>
         <!-- 公共分页 开始 -->
        <div>
          <div class="paging">
            <!-- undisable这个class是否存在，取决于数据属性hasPrevious -->
            <a
              :class="{ undisable: !courseMap.previous }"
              href="#"
              title="首页"
              @click.prevent="getList(1,8)"
              >首</a
            >
            <a
              :class="{ undisable: !courseMap.previous }"
              href="#"
              title="前一页"
              @click.prevent="getList(courseMap.current - 1,8)"
              >&lt;</a
            >
            <a
              v-for="page in courseMap.pages"
              :key="page"
              :class="{
                current: courseMap.current == page,
                undisable: courseMap.current == page,
              }"
              :title="'第' + page + '页'"
              href="#"
              @click.prevent="getList(page,8)"
              >{{ page }}</a
            >
            <a
              :class="{ undisable: !courseMap.hasNext }"
              href="#"
              title="后一页"
              @click.prevent="getList(courseMap.current + 1,8)"
              >&gt;</a
            >
            <a
              :class="{ undisable: !courseMap.hasNext }"
              href="#"
              title="末页"
              @click.prevent="getList(courseMap.pages,8)"
              >末</a
            >
            <div class="clear" />
          </div>
        </div>
        <!-- 公共分页 结束 -->
      </section>
    </section>
    <!-- /课程列表 结束 -->
  </div>
</template>

<script>
//引入调用teacher.js文件
import courseApi from "@/api/course";
export default {
  //赋值区
  data() {
    return {
      courseQuery:{  //查询课程的分类
        subjectParentId:'',
        subjectId:'',
        viewCount:'',
        gmtCreate:'',
        price:''
      },
      oneIndex:-1,
      twoIndex:-1,
      twoSubject:[],  //二级类别list
      subjectList:[],  //全部subject类别list
      current: 1, //当前页
      pagesize: 8, //每页记录数
      courseMap: {
        //返回值
        records: [], //集合
        total: 0,
        current:0,
        pages:0,
        size: 0,
        previous: true,
        hasNext: true,
      },
    };
  },
  //开始就执行区
  created() {
    //初始化课程
    this.getInitCourse(this.current, this.pagesize,this.courseQuery);
    //获得全部Subject
    this.getAllSubject()

  },
  //方法区
  methods: {
    //按照销量进行分类
    searchBuyCount(){
         //赋值：1为选中
         this.courseQuery.viewCount='1'
         //清空其他分类
         this.courseQuery.gmtCreate=''
         this.courseQuery.price=''
         this.getInitCourse(this.current, this.pagesize,this.courseQuery)

    },
    //按照时间进行分类
    searchGmtCreate(){
         //赋值：1为选中
         this.courseQuery.gmtCreate='1'
         //清空其他分类
         this.courseQuery.viewCount=''
         this.courseQuery.price=''
         this.getInitCourse(this.current, this.pagesize,this.courseQuery)

    },
    //按照价格进行分类
    searchPrice(){
         //赋值：1为选中
         this.courseQuery.price='1'
         //清空其他分类
         this.courseQuery.gmtCreate=''
         this.courseQuery.viewCount=''
         this.getInitCourse(this.current, this.pagesize,this.courseQuery)

    },
    //按照二级分类查询课程
    selectTwoSubject(subjectId,index){
        this.twoIndex=index
        this.courseQuery.subjectId=subjectId
        this.getInitCourse(this.current, this.pagesize,this.courseQuery)
    },
    //查询课程分类下的二级分类，并按照一级分类查询课程
    selectOneSubject(parentid,index){
      //把二级分类清空
      this.courseQuery.subjectId=''
      this.twoIndex=-1
      //一级类别被选中的状态
       this.oneIndex=index
       //一级分类查询相应的课程
       this.courseQuery.subjectParentId=parentid
       this.getInitCourse(this.current, this.pagesize,this.courseQuery);
        //根据一级分类的id求出二级分类
        for(let i=0;i<this.subjectList.length;i++){
            var ones=this.subjectList[i]
            if(parentid==ones.id){
              this.twoSubject=ones.children
            }
        }
        
    },
    //查询所有subject
    getAllSubject() {
      courseApi.getAllSubject()
        .then((response) => {
          this.subjectList = response.data.data.subjectList;
        })
        .catch((error) => {
          console.log(error);
        });
    },
    //分页查询课程的首次查询方法
    getInitCourse(current, pagesize,courseQuery) {
      courseApi.getCourseList(current, pagesize,courseQuery)
        .then((response) => {
          this.courseMap = response.data.data.courseMap;
        })
        .catch((error) => {
          console.log(error);
        });
    }
  }

};
</script>

<style scoped>
  .active {
    background: #bdbdbd;
  }
  .hide {
    display: none;
  }
  .show {
    display: block;
  }
</style>